package com.example.xyr.myapplication.ui

import android.graphics.Bitmap
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import com.amap.api.maps.AMap
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.MarkerOptions
import com.example.xyr.myapplication.R
import com.example.xyr.myapplication.base.BaseActivity
import com.example.xyr.myapplication.utils.*
import com.jrmf360.tools.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_map.*
import kotlinx.android.synthetic.main.titlebar_layout.*


/**
 * Created by ylcf on 2018/8/30.
 */
class MapActivity : BaseActivity(), View.OnClickListener {

    private lateinit var latlon: LatlonUtils

    private var aMap: AMap? = null

    private lateinit var mlatLng: LatLng

    private lateinit var address: String

    private var handler: Handler = object : Handler() {
        override fun dispatchMessage(msg: Message?) {
            if (msg?.what == 1) {
                setMarks(mlatLng, BitmapUtils.getInstance().getmNickName(), BitmapUtils.getInstance().getmAvatar());
            }
        }
    }


    override fun getLayout(): Int = R.layout.activity_map

    override fun init() {
        //初始化位置
        latlon = LatlonUtils.getInstance(this)
        mlatLng = latlon.latlng
        address = latlon.address
        aMap = map_view.map
    }

    override fun initListener() {
        titleBar.ivBack.setOnClickListener(this)
        iv_location.setOnClickListener(this)
        //标记点击事件
        aMap?.setOnMarkerClickListener { marker ->
            if (marker.snippet.equals(BitmapUtils.getInstance().getmNickName())) {
                ToastUtil.showLongToast(this@MapActivity, address)
            } else {
                val otherAddress = latlon.otherAddress
                if (StringUtil.isNotEmpty(otherAddress)) {
                    ToastUtil.showLongToast(this@MapActivity, otherAddress)
                }
            }
            false
        }
    }

    override fun initDate(intent: MyIntent?) {
        titleBar.setTitle("恋人位置")
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        map_view.onCreate(savedInstanceState)
        setMarks(mlatLng, BitmapUtils.getInstance().getmNickName(), BitmapUtils.getInstance().getmAvatar());
        setOtherMarks(MainActivity.otherAddress);
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> finish()
        //更新位置
            R.id.iv_location -> {
                val activity = ActivityManger.instance.findActivity(MainActivity::class.java)
                if (activity != null) {
                    activity.getOtherInfo()
                    ToastUtil.showLongToast(this, "位置更新")
                }
                BitmapUtils.getInstance().upDateUserInfo()
                setMarks(mlatLng, BitmapUtils.getInstance().getmNickName(), BitmapUtils.getInstance().getmAvatar())
            }
        }
    }

    /**
     * 更新对方位置
     *
     * @param address
     */
    fun setOtherMarks(address: String) {
        if (StringUtil.isNotEmpty(address)) {
            val split = MainActivity.otherAddress.split("&".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            if (split.size > 1) {
                val d = split[0].toDouble()
                val d1 = split[1].toDouble()
                val oLatlng = LatLng(d, d1)
                latlon.getAddressByLatlng(oLatlng)
                setMarks(oLatlng, MainActivity.otherNickName, BitmapUtils.getInstance().getoAvatar())
            }
        }
    }

    /**
     * 自定义mark
     */
    private fun setMarks(latLng: LatLng?, userName: String, avatar: Bitmap) {
        if (latLng != null) {
            LogUtil.e("tag", "设置marks》》》$userName")
            val markerOption = MarkerOptions()
            markerOption.position(latLng)
            markerOption.title("  ღ").snippet(userName)

            markerOption.draggable(true)//设置Marker可拖动
            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapUtils.scaleBitmap(avatar, 70, 70)))

            // 将Marker设置为贴地显示，可以双指下拉地图查看效果
            markerOption.isFlat = true//设置marker平贴地图效果
            aMap?.addMarker(markerOption)

        } else {
            if (userName == BitmapUtils.getInstance().getmNickName()) {
                handler.sendEmptyMessageDelayed(1, 1500)
            }
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        map_view.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        map_view.onResume()
    }

    override fun onPause() {
        super.onPause()
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        map_view.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        map_view.onSaveInstanceState(outState)
    }
}